﻿using AyaWo.Commen;
using AyaWo.DAL;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiuLetters
{
    public class XiuLettersSpler : ISpler, Spler
    {
        bool _lock = true;
        Queue<string> qdm = new Queue<string>();

        public XiuLettersSpler()
        {
            this.DatName = "XiuLetters";
            this.SplerName = "秀虫";
        }

        #region 执行 +void Initiali()
        /// <summary>
        /// 执行
        /// </summary>
        public void Initiali()
        {
            try
            {
                if (_lock)
                {
                    _lock = false;
                    string hostUrl = "http://newhouse.xmhouse.com/dongtai/8736.html";
                    //内容链接采集
                    ListUrl(hostUrl);
                    foreach (string dm in qdm)
                    {
                        spidercontent content = GetContent(dm);
                        if (content == null || content.Title.IsNullOrWhiteSpace() || content.Content.IsNullOrWhiteSpace())
                            return;
                        if (spcontentService.LinkCanInserts(dm, 2))
                            spcontentService.AddContent(content);
                    }
                    _lock = true;
                }
            }
            catch (Exception ex)
            {
                Logser.Error(ex.Message + "");
            }
        }
        #endregion

        #region 获取内容 +spidercontent GetContent(string link)
        /// <summary>
        /// 获取内容
        /// </summary>
        /// <param name="link">网站链接</param>
        /// <returns></returns>
        spidercontent GetContent(string link)
        {
            try
            {
                var NO = CommonHelper.CreateNo();
                spidercontent sp = new spidercontent() { ID =NO, Link = link, CreateTime = DateTime.Now, typ = 2 };
                string docText = HtmlHelper.GetHtmlContent(link, "gb2312");
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                if (doc == null)
                    return null;
                var htmlNode = doc.DocumentNode;
                var title = htmlNode.SelectSingleNode("//div[@class=\"news-content\"]//h2");
                if (title != null)
                {
                    sp.Title = title.InnerText.Replace("\\n", "").Trim();
                }
                var time = htmlNode.SelectSingleNode("//span[@class=\"C-Q-gray\"]");
                if (time != null)
                {
                    sp.BuildTime = time.InnerText.Replace("\\n", "").Trim();
                }
                var content = htmlNode.SelectSingleNode("//div[@id=\"contenttowb\"]");
                if (content.InnerHtml.NotNullOrWhiteSpace())
                {
                    ///处理下载图片
                    HtmlNodeCollection img = htmlNode.SelectNodes("//div[@id=\"contenttowb\"]//img");
                    if (img != null && img.Count() > 0)
                    {
                        string baseurl = link.Substring(0, link.LastIndexOf('/')) + "/";
                        string imgurl = null; string imgName = null;
                        foreach (HtmlNode child in img)
                        {
                            imgName = child.Attributes["oldsrc"].Value;
                            imgurl = baseurl + imgName;
                            string newsrc = HtmlHelper.DownLoadImg(imgurl, imgName, NO);
                            child.SetAttributeValue("_fcksavedurl", "");
                            child.SetAttributeValue("style", "");
                            child.SetAttributeValue("src", newsrc);
                        }
                    }
                    content.SelectSingleNode("//div[@id=\"shareBox\"]").InnerHtml = "";
                    content.SelectSingleNode("//div[@id=\"pageList\"]").InnerHtml = "";
                    sp.Content = content.InnerHtml.Replace("\\n", "").Trim();
                }
                return sp;
            }
            catch (Exception ex)
            {
                Logser.Error("秀虫-GetContent: " + ex.Message);
                return null;
            }
        }
        #endregion

        #region 内容链接采集 +void ListUrl(string hostUrl)
        /// <summary>
        /// 内容链接采集
        /// </summary>
        /// <param name="hostUrl"></param>
        private void ListUrl(string hostUrl)
        {
            try
            {
                string docText = HtmlHelper.GetHtmlContent(hostUrl);
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                if (doc == null)
                    return;
                var ul = doc.DocumentNode.SelectNodes("//ul[@class=\"c\"]//li//div[@class=\"r_b\"]//a");
                if (ul != null && ul.Count() > 0)
                {
                    HtmlAttribute href = null;
                    string urlLink = null;
                    foreach (var t in ul)
                    {
                        href = t.Attributes["href"];
                        if (href != null)
                        {
                            urlLink = href.Value;
                            if (spcontentService.CanInsert(urlLink))
                            {
                                qdm.Enqueue(urlLink);
                            }
                        }
                    }
                }
                var divb2 = doc.DocumentNode.SelectNodes("//div[@class=\"b2\"]//ul[@class=\"c\"]//li//a");
                if (divb2 != null && divb2.Count() > 0)
                {
                    HtmlAttribute href = null;
                    string urlLink = null;
                    foreach (var t in divb2)
                    {
                        href = t.Attributes["href"];
                        if (href != null)
                        {
                            urlLink = href.Value;
                            if (spcontentService.CanInsert(urlLink))
                            {
                                qdm.Enqueue(urlLink);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logser.Error("秀虫 ListUrl: " + ex.Message);
            }
        } 
        #endregion
    }
}
